//
//  WKUserBooksListVC.m
//  WKMaybell
//
//  Created by 何文虎 on 2018/12/25.
//  Copyright © 2018 何文虎. All rights reserved.
//

#import "WKUserBooksListVC.h"
#import "WKUserBooksListCell.h"
#import "WKUserBooksListModel.h"
#import "WKUserBooksHeaderView.h"

@interface WKUserBooksListVC ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataSource;
@end

@implementation WKUserBooksListVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViews];
    [self requesData];
}

- (void)initViews{
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self.naviBar configMynavibarStyle:SSNavigationBarWhiteStyle];
    self.naviBar.backgroundColor = [UIColor clearColor];
    [self.naviBar hiddenNaviBarLine];
    self.naviBar.title = @"日记历程";
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[WKUserBooksListCell class] forCellReuseIdentifier:NSStringFromClass([WKUserBooksListCell class])];
    [self.view addSubview:_tableView];
    
    CGFloat headerHeight = NavBarHeight+12+20+48 +44;
    WKUserBooksHeaderView *headerView = [[WKUserBooksHeaderView alloc]initWithFrame:CGRectMake(0,0, ScreenWidth, headerHeight)];
    _tableView.tableHeaderView = headerView;
    [self.view bringSubviewToFront:self.naviBar];
    
    if (@available(iOS 11.0, *)) {
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else{
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WKUserBooksListModel *model = [self.dataSource objectAtIndex:indexPath.row];
    WKUserBooksListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([WKUserBooksListCell class]) forIndexPath:indexPath];
     [cell displayUserBooksListCellWithModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    WKUserBooksListModel *model = [self.dataSource objectAtIndex:indexPath.row];
    CGFloat  height = [WKUserBooksListCell hyb_heightForTableView:tableView config:^(UITableViewCell *sourceCell) {
        WKUserBooksListCell *cell = (WKUserBooksListCell*) sourceCell;
        [cell displayUserBooksListCellWithModel:model];
        
    } cache:^NSDictionary *{
        return @{kHYBCacheUniqueKey: model.bookId,
                 kHYBCacheStateKey : @"",
                 // 如果设置为YES，若有缓存，则更新缓存，否则直接计算并缓存
                 // 主要是对社交这种有动态评论等不同状态，高度也会不同的情况的处理
                 kHYBRecalculateForStateKey : @(NO) // 标识不用重新更新
                 };
    }];
    
    return height;
}


- (void)requesData{
    NSArray *dataArr = @[@{@"bookId":@"1",@"bookType":@"1",@"time":@"11月25日",@"count":@"12",@"title":@"悟空祛痘第168天",@"content":@"刘若英坦承自己有一首歌，让她唱起来最为心痛，因为她总觉得是在唱自己的故事；这首很多人多都很熟悉的歌曲《后来》，歌中描述历经多次失败恋情，后来才明了因为年少轻狂，轻易放弃的一段感情，是多么的可惜。这是写给爱后悔的人唱的，后悔是人的本能，但老后悔就没劲了",@"zanNum":@"300",@"commentNum":@"167",@"imgs":@[@"http://5b0988e595225.cdn.sohucs.com/images/20180501/3f16c9b0734942b7ab37d0ec06d091cb.jpeg",@"http://5b0988e595225.cdn.sohucs.com/q_70,c_zoom,w_640/images/20180706/de288c57e9754be3a958834487d0651f.jpeg",@"http://s4.sinaimg.cn/mw690/001GI2hTgy6E5IWQInN33&690",@"http://img.mp.sohu.com/q_mini,c_zoom,w_640/upload/20170805/2259f0939fe14b6f8f61dc15a910d9ec_th.jpg"]},@{@"bookId":@"12",@"bookType":@"2",@"time":@"11月25日",@"count":@"12",@"title":@"悟空祛痘第168天",@"":@"刘若英坦承自己有一首歌，让她唱起来最为心痛，因为她总觉得是在唱自己的故事；这首很多人多都很熟悉的歌曲《后来》，歌中描述历经多次失败恋情，后来才明了因为年少轻狂，轻易放弃的一段感情，是多么的可惜。这是写给爱后悔的人唱的，后悔是人的本能，但老后悔就没劲了",@"zanNum":@"300",@"commentNum":@"167",@"videoUrl":@"https://xingchenxueyuan.oss-cn-beijing.aliyuncs.com/20180521/%E5%B0%8F%E6%B0%94%E6%B3%A1%E6%88%90%E7%89%87%E6%AD%A3%E5%B8%B8%E8%82%8C%E8%82%A4%E6%B5%81%E7%A8%8B.mp4",@"videoImgUrl":@"http://www.chinairn.com/UserFiles/image/20171020/20171020163347_2308.jpg"}];
    
    self.dataSource = [NSMutableArray arrayWithCapacity:0];
    self.dataSource = [WKUserBooksListModel mj_objectArrayWithKeyValuesArray:dataArr];
    
    [self.tableView reloadData];
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
     CGFloat headerHeight = 12+20+48 +44;
    if (scrollView.contentOffset.y >=headerHeight) {
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
        self.naviBar.backgroundColor = [UIColor whiteColor];
        [self.naviBar showNaviBarLine];
        [self.naviBar showUserinformation];
        [self.naviBar configMynavibarStyle:SSNavigationBarBlackStyle];
        
    }else{
        [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
        self.naviBar.backgroundColor = [UIColor clearColor];
        [self.naviBar hiddenNaviBarLine];
        [self.naviBar hiddenUserinformation];
        [self.naviBar configMynavibarStyle:SSNavigationBarWhiteStyle];
        
    }
}



@end
